﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;
using System.IO;
#endregion Using Statements

namespace CakeRaid
{
    class Tower_Rad : Tower
    {
        #region Attributes
        protected float fireAngle;
        protected Enemy enemyToAttack;
        protected SoundEffect towerFire;
        #endregion Attributes

        #region Constructor
        public Tower_Rad(ImageObject io, float fireRadius, int health, int cost, int damage, float speed, float bSpeed, Texture2D bt, Attacktype attackType, SoundEffect towerFire)
            : base(io, fireRadius, health, cost, damage, speed, bSpeed, bt, attackType)
        {
            type = Var.TowerType.Rad;
            StartHealth = CurrentHealth = Var.RAD_TOWER_HEALTH;
            this.towerFire = towerFire;
        }
        #endregion Constructor

        #region Methods
        public override void Activate()
        {
            if (!placing && IsActive)
            {
                if (eventTimer.ElapsedMilliseconds >= (Speed / Var.GAME_SPEED))
                {
                    fireAngle = -1000; // random number to make sure it should fire.

                    #region Attack based on AttackType
                    if (enemiesInRange.Count > 0)
                    {
                        // Holds an enemy until it knows what one to attack (ex: if looking for strongest in-range enemies)
                        enemyToAttack = enemiesInRange.First(); // To have something to check against

                        switch (attackType)
                        {
                            #region Attacktype.None
                            case Attacktype.None:
                                enemyToAttack = enemiesInRange[rand.Next(enemiesInRange.Count)];
                                break;
                            #endregion Attacktype.None

                            #region Attacktype.Fastest
                            case Attacktype.Fastest:
                                foreach (Enemy enemy in enemiesInRange)
                                {
                                    if (enemy.Speed > enemyToAttack.Speed)
                                        enemyToAttack = enemy;
                                }
                                break;
                            #endregion Attacktype.Fastest

                            #region Attacktype.Slowest
                            case Attacktype.Slowest:
                                foreach (Enemy enemy in enemiesInRange)
                                {
                                    if (enemy.Speed < enemyToAttack.Speed)
                                        enemyToAttack = enemy;
                                }
                                break;
                            #endregion Attacktype.Slowest

                            #region Attacktype.Strongest
                            case Attacktype.Strongest:
                                foreach (Enemy enemy in enemiesInRange)
                                {
                                    if (enemy.CurrentHealth > enemyToAttack.CurrentHealth)
                                        enemyToAttack = enemy;
                                }
                                break;
                            #endregion Attacktype.Strongest
                            
                            #region Attacktype.Weakest
                            case Attacktype.Weakest:
                                foreach (Enemy enemy in enemiesInRange)
                                {
                                    if (enemy.CurrentHealth < enemyToAttack.CurrentHealth)
                                        enemyToAttack = enemy;
                                }
                                break;
                            #endregion Attacktype.Weakest
                            
                            #region Attacktype.First
                            case Attacktype.First:
                                foreach (Enemy enemy in enemiesInRange)
                                {
                                    if (enemy.CurrentTile > enemyToAttack.CurrentTile)
                                        enemyToAttack = enemy;
                                }
                                break;
                            #endregion Attacktype.First

                            #region Attacktype.Last
                            case Attacktype.Last:
                                foreach (Enemy enemy in enemiesInRange)
                                {
                                    if (enemy.CurrentTile < enemyToAttack.CurrentTile)
                                        enemyToAttack = enemy;
                                }
                                break;
                            #endregion Attacktype.Last
                        }
                        fireAngle = enemyToAttack.CheckWhereIWillBe(this); // attacks enemy chosen
                        enemiesInRange.Clear();
                    }
                    #endregion Attack based on AttackType

                    if (fireAngle != -1000)
                    {
                        eventTimer.Restart();
                        towerFire.Play();
                        bullets.Add(new Bullet(new ImageObject(bulletTexture, (int)Center.X, (int)Center.Y, Var.BULLET_SIZE, Var.BULLET_SIZE, Image.SpriteBatch), fireAngle, Damage, bulletSpeed));
                    }
                }

                bullets.ForEach(bullet => bullet.Move());
                if (bullets.Count > 10)
                {
                    bullets.RemoveRange(0, 1);
                }
            }
        }
        #endregion Methods

        #region Draw
        public override void Draw(GameTime gameTime)
        {
            if (IsActive)
            {
                if (CurrentHealth > ((Var.RAD_TOWER_HEALTH / 4) * 3) && CurrentHealth <= Var.RAD_TOWER_HEALTH)
                {
                    base.Draw(0, 0);
                }
                else if (CurrentHealth > ((Var.RAD_TOWER_HEALTH / 4) * 2) && CurrentHealth <= ((Var.RAD_TOWER_HEALTH / 4) * 3))
                {
                    base.Draw(1, 0);
                }
                else if (CurrentHealth > ((Var.RAD_TOWER_HEALTH / 4) * 1) && CurrentHealth <= ((Var.RAD_TOWER_HEALTH / 4) * 2))
                {
                    base.Draw(2, 0);
                }
                else if (CurrentHealth > 0 && CurrentHealth <= ((Var.RAD_TOWER_HEALTH / 4) * 1))
                {
                    base.Draw(3, 0);
                }
                bullets.ForEach(bullet => bullet.Draw(0, 0));
                healthBar.Draw();
            }
        }
        #endregion Draw
    }
}